#include "main.h"
#include "system.h"
UART_HandleTypeDef BluetoothUart;
UART_HandleTypeDef CommunicationUart;
UART_HandleTypeDef LogUart;

uint8	Communication_Uart_Buf[10]={0};
uint8	Bluetooth_Uart_Buf[10]={0};

void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};	
  if (huart->Instance == Bluetooth_Uart)
  {
    
    __HAL_RCC_USART3_CLK_ENABLE();

    GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF1_USART3;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    
    HAL_NVIC_SetPriority(USART3_6_IRQn,4, 0);
    HAL_NVIC_EnableIRQ(USART3_6_IRQn);
  }
  else if (huart->Instance == Communication_Uart)
  {
    
    __HAL_RCC_USART4_CLK_ENABLE();
    GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF0_USART4;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    
    HAL_NVIC_SetPriority(USART3_6_IRQn,5, 1);
    HAL_NVIC_EnableIRQ(USART3_6_IRQn);
  }
  else if (huart->Instance == Log_Uart)
  {
	  
	  __HAL_RCC_USART6_CLK_ENABLE();
 
	  GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
	  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	  GPIO_InitStruct.Pull = GPIO_NOPULL;
	  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	  GPIO_InitStruct.Alternate = GPIO_AF2_USART6;
	  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 
	}

}

void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
{
  	

  if (huart->Instance == Bluetooth_Uart)
  {

    
    __HAL_RCC_USART3_CLK_DISABLE();
    HAL_GPIO_DeInit(GPIOC, GPIO_PIN_4| GPIO_PIN_5);
    
    HAL_NVIC_DisableIRQ(USART3_6_IRQn);
  }
  else if (huart->Instance == Communication_Uart)
  {

    
  	__HAL_RCC_USART4_CLK_DISABLE();
    HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10| GPIO_PIN_11);
    
    HAL_NVIC_DisableIRQ(USART3_6_IRQn);
  }
}

void Bluetooth_UART_Init(void)
{
 	
  BluetoothUart.Instance = Bluetooth_Uart;
  BluetoothUart.Init.BaudRate = 9600;
  BluetoothUart.Init.WordLength = UART_WORDLENGTH_8B;
  BluetoothUart.Init.StopBits = UART_STOPBITS_1;
  BluetoothUart.Init.Parity = UART_PARITY_NONE;
  BluetoothUart.Init.Mode = UART_MODE_TX_RX;
  BluetoothUart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  BluetoothUart.Init.OverSampling = UART_OVERSAMPLING_16;
  BluetoothUart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  BluetoothUart.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  BluetoothUart.gState =HAL_UART_STATE_RESET;
  if (HAL_UART_Init(&BluetoothUart) != HAL_OK)
  {
    Error_Handler();
  }
  HAL_UART_Receive_IT(&BluetoothUart,(uint8_t *)Bluetooth_Uart_Buf,1);

}

void Commu_UART_Init(void)
{
  CommunicationUart.Instance = Communication_Uart;
  CommunicationUart.Init.BaudRate = 38400;
  CommunicationUart.Init.WordLength = UART_WORDLENGTH_8B;
  CommunicationUart.Init.StopBits = UART_STOPBITS_1;
  CommunicationUart.Init.Parity = UART_PARITY_NONE;
  CommunicationUart.Init.Mode = UART_MODE_TX_RX;
  CommunicationUart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  CommunicationUart.Init.OverSampling = UART_OVERSAMPLING_16;
  CommunicationUart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  CommunicationUart.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&CommunicationUart) != HAL_OK)
  {
    Error_Handler();
  }
  HAL_UART_Receive_IT(&CommunicationUart,(uint8_t *)Communication_Uart_Buf,1);
}

void Print_UART_Init(void)
{
  LogUart.Instance = Log_Uart;
  LogUart.Init.BaudRate = 115200;
  LogUart.Init.WordLength = UART_WORDLENGTH_8B;
  LogUart.Init.StopBits = UART_STOPBITS_1;
  LogUart.Init.Parity = UART_PARITY_NONE;
  LogUart.Init.Mode = UART_MODE_TX_RX;
  LogUart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  LogUart.Init.OverSampling = UART_OVERSAMPLING_16;
  LogUart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  LogUart.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&LogUart) != HAL_OK)
  {
    Error_Handler();
  }
}


uint8 Bluetooth_RX_BUF[sizeof(ReceDataInfor)]={0};
ReceDataInfor*Bluetooth_ReceData=(ReceDataInfor*)Bluetooth_RX_BUF;

uint8 CollectBoard_RX_BUF[sizeof(ReceDataInfor)]={0};
ReceDataInfor* CollectBoard_ReceData=(ReceDataInfor*)CollectBoard_RX_BUF;


uint8 flag = 0;
uint8 wp = 0;
uint8 ResppackRece = 0;
uint8 lastbyte = 0;
uint8 nowbyet = 0;

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  uint8 temp = 0;
 
  if (huart->Instance == Bluetooth_Uart)
  {
  	  if(Bluetooth_ReceData->processed==1)
  	  {
		  Bluetooth_ReceData->buf[Bluetooth_ReceData->len++]=Bluetooth_Uart_Buf[0];
		  if(Bluetooth_ReceData->len>=Uart_ReceBufLen)
		  {
			  Bluetooth_ReceData->len=0;
		  }
  	  }
  }
  else  if (huart->Instance == Communication_Uart)
  {

	CollectBoard_ReceData->buf[CollectBoard_ReceData->len++]=Communication_Uart_Buf[0];
	if(CollectBoard_ReceData->len>=Uart_ReceBufLen)
	{
		CollectBoard_ReceData->len=0;
	}

  }
}

#if 0
void  USART2_IRQHandler(void)
{
	
	HAL_UART_IRQHandler(&CommunicationUart);
	HAL_UART_Receive_IT(&CommunicationUart,(uint8_t *)Communication_Uart_Buf,1);
}


void  USART1_IRQHandler(void)
{
	
	HAL_UART_IRQHandler(&BluetoothUart);
	HAL_UART_Receive_IT(&BluetoothUart,(uint8_t *)Bluetooth_Uart_Buf,1);

	
}
#endif
void	USART3_6_IRQHandler(void)
{
	
    if ( (__HAL_UART_GET_FLAG (&CommunicationUart, UART_FLAG_RXNE) != RESET) )
    {
    	
		HAL_UART_IRQHandler(&CommunicationUart);
		HAL_UART_Receive_IT(&CommunicationUart,(uint8_t *)Communication_Uart_Buf,1);

	}
	else if( __HAL_UART_GET_FLAG (&BluetoothUart, UART_FLAG_RXNE) != RESET) 
	{
		
		HAL_UART_IRQHandler(&BluetoothUart);
		HAL_UART_Receive_IT(&BluetoothUart,(uint8_t *)Bluetooth_Uart_Buf,1);

	}
	/*else
	{
		
	}*/

}


uint8 	volatile Debug_Send_Data_Flag = 0;
uint16 	volatile Debug_Send_Data_Value = 0;
uint8 	volatile Debug_Send_Data_Cnt = 0;

int fputc(int ch, FILE *f)
{
  
  /*Debug_Send_Data_Value = (uint8_t)ch << 1;
  Debug_Send_Data_Value &= ~0x0001;    
  Debug_Send_Data_Value |= 0x0003 << 9; 
  Debug_Send_Data_Cnt = 0;
  Debug_Send_Data_Flag = 1;
  while (Debug_Send_Data_Flag == 1)
  {
  
  }*/

  HAL_UART_Transmit(&LogUart, (uint8_t *)&ch, 1, 0xffff);
  return ch;

}


IWDG_HandleTypeDef	hiwdg;

void WatchdogInitial(void)
{
	
	hiwdg.Instance = IWDG;
	hiwdg.Init.Prescaler = IWDG_PRESCALER_128;
	hiwdg.Init.Window = 4095;
	hiwdg.Init.Reload = 4095;
	HAL_IWDG_Init(&hiwdg);
	
}

void FeedWatchDog(void)
{
	HAL_IWDG_Refresh(&hiwdg);
}

